
%%  Function to convert from real to binary array 

% par = Population member (floating point).
% varhi = specific problem higher bound. 
% varlo = specific problem lower bound.
% nbits = number of bits per real parameter.

% developed by Omar Arif Abdul-Rahman 
% 2012

function f = gaencode( par, varhi, varlo, nbits)
[popsize,npar] = size (par); % extract dimension 
pnorm = (par - varlo)/(varhi-varlo); 
quant = floor ( pnorm* 2^nbits);
quant = reshape(quant,1, popsize*npar);
t = 1; x = zeros(popsize*npar,nbits);
for j = 1:popsize*npar
for k = nbits:-1:1
if(bitand(quant(1,j), t) )
x (j,k) = 1;
else
x (j,k) = 0;
end;
t = bitshift(t,1);
end;
t = 1;
end;
j = 0;
k = 1;

for n = 1:npar
    L1 = k; L2=n*nbits;
    for m = 1:popsize
        j = j + 1;
        pop(m,L1:L2) = x(j,1:nbits);
    end;
     k = n*nbits+1;
end;

f = pop;
end

